<?php
namespace App\Task;

use EasySwoole\EasySwoole\Crontab\AbstractCronTask;
use EasySwoole\EasySwoole\Logger;

use App\Storage\OnlineClient;
use App\Storage\OnlineUser;

// https://www.easyswoole.com/Cn/BaseUsage/crontab.html
class CheckHeartbeatTask extends AbstractCronTask
{
    /**
     * 运行周期: 十分钟
     *
     * @Author LYC 17750800820@163.com
     * @DateTime 2020-04-24 11:32:11
     * @return string
     */
    public static function getRule(): string
    {
        return '*/1 * * * *';
    }

    /**
     * 任务命名
     *
     * @Author LYC 17750800820@163.com
     * @DateTime 2020-04-24 11:32:32
     * @return string
     */
    public static function getTaskName(): string
    {
        return  'checkHeartbeat';
    }

    /**
     * 清理掉线客户端
     *
     * @Author LYC 17750800820@163.com
     * @DateTime 2020-04-24 11:34:10
     * @param integer $taskId
     * @param integer $workerIndex
     * @return void
     */
    public function run(int $taskId, int $workerIndex)
    {
        // 长时间没有心跳删除在线终端|网页客户端
        OnlineClient::getInstance()->heartbeatCheck();
        OnlineUser::getInstance()->heartbeatCheck();
    }

    public function onException(\Throwable $throwable, int $taskId, int $workerIndex)
    {
        Logger::getInstance()->log(($throwable->getMessage()), Logger::LOG_LEVEL_INFO, 'RECORD');
    }
}
